package com.civicscience;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.android.*;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.Facebook.*;
import com.civicscience.R;

public class MainActivity extends Activity {

	public static final String APP_ID = "129840303769814";
	private final ArrayList<Friend> friends = new ArrayList<Friend>();
	private static final String[] PERMISSIONS = new String[] {
			"publish_stream", "read_stream", "offline_access", "user_about_me",
			"user_birthday" };

	private TextView mText;
	private ListView listView;
	private Facebook facebook;
	private AsyncFacebookRunner asyncRunner;
	private Handler mHandler = new Handler();
	private ProgressDialog mSpinner;
	private FriendsArrayAdapter friendsArrayAdapter;
	private Activity activity = this;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature( Window.FEATURE_NO_TITLE );
		getWindow().addFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN );

		if (APP_ID == null) {
			Util.showAlert(this, "Warning",
					"Facebook Application ID must be set...");
		}

		// Initialize the content view
		setContentView(R.layout.login);
		
	    //
		Button button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setAction("login");
				startActivity(intent);
			}
		});
	}

	private final class LoginDialogListener implements
			com.facebook.android.Facebook.DialogListener {

		/**
		 * Called when the dialog has completed successfully
		 */
		public void onComplete(Bundle values) {
			// Process onComplete
			Log.d("FB Sample App", "LoginDialogListener.onComplete()");
			// Dispatch on its own thread
			mHandler.post(new Runnable() {
				public void run() {
					mText.setText("Facebook login successful. Press Menu...");
				}
			});
		}

		/**
 *
 */
		public void onFacebookError(FacebookError error) {
			// Process error
			Log.d("FB Sample App", "LoginDialogListener.onFacebookError()");
		}

		/**
 *
 */
		public void onError(DialogError error) {
			// Process error message
			Log.d("FB Sample App", "LoginDialogListener.onError()");
		}

		/**
 *
 */
		public void onCancel() {
			// Process cancel message
			Log.d("FB Sample App", "LoginDialogListener.onCancel()");
		}
	}
}